package ltd.newbee.mall.service.impl;

import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.dao.ScheduleMapper;
import ltd.newbee.mall.entity.Film;
import ltd.newbee.mall.entity.Schedule;
import ltd.newbee.mall.service.ScheduleService;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class Schedulelmpl implements ScheduleService {

    @Autowired
    ScheduleMapper scheduleMapper;


    @Override
    public PageResult selectAll(PageQueryUtil pageUtil) {
        List<Schedule> schedules = scheduleMapper.selectAll(pageUtil);
        int total = scheduleMapper.getTotalSchedule();
        PageResult pageResult = new PageResult(schedules, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public List<Schedule> selectGroupFilm(int filmId) {
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.add(Calendar.MINUTE,-20); //设置提前20分钟
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=df.format(now.getTime());
        System.out.println("time:"+time);
        return scheduleMapper.selectGroupFilm(filmId,time);
    }

    @Override
    public List<Schedule> selectFilm(int filmId) {
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.add(Calendar.MINUTE,-20); //设置提前20分钟
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newtime=df.format(now.getTime());
        now.add(Calendar.DAY_OF_MONTH,1); //设置提前1天
        String lasttime=df.format(now.getTime());
        return scheduleMapper.selectFilm(filmId,newtime,lasttime);
    }

    @Override
    public String saveSchedule(Schedule schedule) {
        if (scheduleMapper.insertSelective(schedule) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String UpdateSchedule(Schedule schedule) {
        if (scheduleMapper.updateByPrimaryKey(schedule) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public boolean deleteSchedule(Integer[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除分类数据
        return scheduleMapper.deleteSchedule(ids) > 0;
    }

}
